/*
* Copyright (c)  2017, ueboot.com
* All rights reserved.
* 2017-11-05 18:13:23
*/
package com.ueboot.weishop.api.controller.goods;

import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.api.controller.goods.vo.*;
import com.ueboot.weishop.entity.goods.GoodsSalesSpecEntity;
import com.ueboot.weishop.service.goods.GoodsSalesSpecService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by yangkui  on 2017-11-05 18:13:23.
 *
 * @author yangkui
 * @since 2.0 by autocode
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/public/goodsSalesSpec")
public class GoodsSalesSpecController {

    @Autowired
    private GoodsSalesSpecService goodsSalesSpecService;


    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<GoodsSalesSpecResp>> page(@PageableDefault(value = 15, sort = {"id"}, direction = Sort.Direction.DESC)
                                                           Pageable pageable, @RequestBody(required = false) GoodsSalesSpecFindReq req) {
        Page<GoodsSalesSpecEntity> entities = goodsSalesSpecService.findBy(pageable);
        Page<GoodsSalesSpecResp> body = entities.map(entity -> {
            GoodsSalesSpecResp resp = new GoodsSalesSpecResp();
            BeanUtils.copyProperties(entity, resp);
            return resp;
        });

        return new Response<>(body);
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Response<GoodsSalesSpecResp> get(@PathVariable Long id) {
        GoodsSalesSpecEntity entity = goodsSalesSpecService.get(id);
        GoodsSalesSpecResp resp = new GoodsSalesSpecResp();
        BeanUtils.copyProperties(entity, resp);
        return new Response<>(resp);
    }

    @RequestMapping(value = "/find/spec", method = RequestMethod.POST)
    public Response<List<GoodsSalesSpecPriceResp>> findSpec(@RequestBody(required = false) GoodsSalesSpecReq req) {
        String specValueFullPathValue = req.getCarMode() + "/";
        if (StringUtils.isNotEmpty(req.getDealer())) {
            specValueFullPathValue += req.getDealer();
        }
        List<GoodsSalesSpecEntity> goodsSalesSpecEntities = goodsSalesSpecService.findSpecAndGoodsId(specValueFullPathValue, req.getGoodsInfoId());
        List<GoodsSalesSpecPriceResp> list = new ArrayList<>();
        for (GoodsSalesSpecEntity goodsSalesSpecEntity : goodsSalesSpecEntities) {
            GoodsSalesSpecPriceResp goodsSalesSpecPriceResp = new GoodsSalesSpecPriceResp();
            goodsSalesSpecPriceResp.setName(goodsSalesSpecEntity.getName());
            goodsSalesSpecPriceResp.setValue(goodsSalesSpecEntity.getId().toString());
            goodsSalesSpecPriceResp.setPrice(goodsSalesSpecEntity.getPrice());
            goodsSalesSpecPriceResp.setOriginPrice(goodsSalesSpecEntity.getOriginPrice());
            list.add(goodsSalesSpecPriceResp);
        }
        return new Response<>(list);
    }

    @RequestMapping(value = "/find/spec/name", method = RequestMethod.POST)
    public Response<List<GoodsSalesSpecResp>> findSpecAndName(@RequestBody GoodsSalesSpecPriceReq req) {
        List<GoodsSalesSpecEntity> goodsSalesSpecEntities = goodsSalesSpecService.findSpecAndName(req.getSpecValueFullPathValue(), req.getName());
        List<GoodsSalesSpecResp> list = new ArrayList<>();
        for (GoodsSalesSpecEntity goodsSalesSpecEntity : goodsSalesSpecEntities) {
            GoodsSalesSpecResp goodsSalesSpecResp = new GoodsSalesSpecResp();
            BeanUtils.copyProperties(goodsSalesSpecEntity, goodsSalesSpecResp);
            list.add(goodsSalesSpecResp);
        }
        return new Response<>(list);
    }

}
